// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.prefs.connectivity.models;

import java.util.ArrayList;
import java.util.List;

import org.dengues.commons.connectivity.EDatabaseType;
import org.dengues.commons.connectivity.ESubDatabaseType;
import org.dengues.commons.connectivity.IConnectivitySettingsConstants;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ConnectivityProvider {

    private static ConnectivityProvider instance;

    private final List<ConnectivityCategory> categories;

    /**
     * Qiang.Zhang.Adolf@gmail.com ConnectivityProvider constructor comment.
     */
    private ConnectivityProvider() {
        super();
        categories = new ArrayList<ConnectivityCategory>();
    }

    public static ConnectivityProvider getInstance() {
        if (instance == null) {
            instance = new ConnectivityProvider();
        }
        return instance;
    }

    public List<ConnectivityCategory> getCategoriesById(ConnectivityCategory parent) {
        List<ConnectivityCategory> list = new ArrayList<ConnectivityCategory>();
        for (EDatabaseType type : EDatabaseType.values()) {
            ConnectivityCategory category = new ConnectivityGenCategory(type);
            category.setParent(parent);
            list.add(category);
        }
        categories.addAll(list);
        return list;
    }

    public ConnectivityCategory getCategoriesById(String id) {
        for (ConnectivityCategory category : categories) {
            String categoryId = getCategoryId(category);
            if (id.equals(categoryId)) {
                return category;
            }
        }
        return null;
    }

    public List<ConnectivityCategory> getRootCategories() {
        List<ConnectivityCategory> list = new ArrayList<ConnectivityCategory>();
        ConnectivityCategory category = new ConnectivityRootCategory();
        list.add(category);
        return list;
    }

    public List<ConnectivityCategory> getSubCategories(ConnectivityCategory parent, EDatabaseType type) {
        List<ConnectivityCategory> list = new ArrayList<ConnectivityCategory>();
        for (ESubDatabaseType subType : ESubDatabaseType.values()) {
            if (subType.getType() == type) {
                ConnectivityCategory category = new ConnectivitySubCategory(parent, subType);
                list.add(category);
            }
        }
        categories.addAll(list);
        return list;
    }

    public String getCategoryId(ConnectivityCategory property) {
        StringBuilder builder = new StringBuilder();
        builder.append(IConnectivitySettingsConstants.PROP_DEFN);
        if (property instanceof ConnectivityProperty) {
            getFullCategoryId(property.getParent(), builder);
        } else {
            getFullCategoryId(property, builder);
        }
        return builder.toString();
    }

    private void getFullCategoryId(ConnectivityCategory category, StringBuilder builder) {
        if (category == null) {
            return;
        }
        if (category.getParent() != null) {
            getFullCategoryId(category.getParent(), builder);
            builder.append(IConnectivitySettingsConstants.PROP_DELIMITER + category.getId());
        } else {
            builder.append(IConnectivitySettingsConstants.PROP_DELIMITER + category.getId());
        }
    }
}
